﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using UtilZ.Dotnet.WindowsDesktopEx.Winform;

namespace UtilZ.Dotnet.WindowsDesktopEx.Winform
{
    /// <summary>
    /// 
    /// </summary>
    public class UCMetroShadeControl32Animal : EllipseRotateLoadingControl, IPartAsynWaitWinform
    {
        #region IPartAsynWait接口
        /// <summary>
        /// 是否已经取消
        /// </summary>
        private bool _isCanceled = false;
        /// <summary>
        /// 获取是否已经取消
        /// </summary>
        public bool Canceled
        {
            get { return this._isCanceled; }
        }

        /// <summary>
        /// 获取或设置提示标题
        /// </summary>
        public string Title { get; set; } = null;

        /// <summary>
        /// 获取或设置提示内容
        /// </summary>
        public string Message { get; set; } = null;

        /// <summary>
        /// 获取或设置是否显示取消按钮
        /// </summary>
        public bool ShowCancel { get; set; } = false;

        /// <summary>
        /// 获取或设置动画背景色
        /// </summary>
        public object ShadeBackground { get; set; } = null;

        /// <summary>
        /// 这招层透明度
        /// </summary>
        public double ShadeOpacity { get; set; }

        /// <summary>
        /// 取消通知事件
        /// </summary>
        public event EventHandler CanceledNotify;

        /// <summary>
        /// 线程锁
        /// </summary>
        private readonly object _monitor = new object();

        /// <summary>
        /// 取消操作
        /// </summary>
        public void Cancel()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(this.Cancel));
                return;
            }

            lock (this._monitor)
            {
                if (this._isCanceled)
                {
                    return;
                }

                this._isCanceled = true;
            }

            var handler = this.CanceledNotify;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        /// <summary>
        /// 开始动画
        /// </summary>
        public void StartAnimation()
        {
            base.StartAnimal();
        }

        /// <summary>
        /// 停止动画
        /// </summary>
        public void StopAnimation()
        {
            base.StopAnimal();
        }

        /// <summary>
        /// 设置信息(保留接口)
        /// </summary>
        /// <param name="para">参数</param>
        public void SetInfo(object para)
        {

        }

        /// <summary>
        /// 重置异步等待框
        /// </summary>
        public void Reset()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(this.Reset));
            }
            else
            {
                this._isCanceled = false;
            }
        }

        /// <summary>
        /// 设置焦点到指定控件,通常是取消按钮上
        /// </summary>
        public void SetFocus()
        {
            this.Focus();
        }

        /// <summary>
        /// 获取UI是否处于设计器模式
        /// </summary>
        public bool UIDesignMode
        {
            get
            {
                bool isDesignMode = false;
                if (this.InvokeRequired)
                {
                    this.Invoke(new Action(() =>
                    {
                        isDesignMode = this.DesignMode;
                    }));
                }
                else
                {
                    isDesignMode = this.DesignMode;
                }

                return isDesignMode;
            }
        }
        #endregion




        /// <summary>
        /// 构造函数初始化
        /// </summary>
        public UCMetroShadeControl32Animal()
            : base()
        {
            base.EllipseSize = 8f;

            this.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.Location = new System.Drawing.Point(122, 2);
            this.Name = "UCMetroShadeControl32Animal";
            this.Size = new System.Drawing.Size(54, 54);
            this.TabIndex = 0;
            this.BackColor = System.Drawing.Color.Transparent;
            this.TabStop = false;
        }


        /// <summary>
        /// 引发 System.Windows.Forms.PictureBox.LoadCompleted 事件
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            var handler = this._load;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        private event EventHandler _load;
        /// <summary>
        /// 在第一次显示控件前发生
        /// </summary>
        event EventHandler IControl.Load
        {
            add
            {
                _load += value;
            }
            remove
            {
                _load -= value;
            }
        }
    }
}
